import json
import time

import openpyxl as openpyxl

from httpRequest.sign_http_request3 import send_request

import xlwt

from IO.read_data import read_season


def set_style(name, height, bold=False):
    style = xlwt.XFStyle()  # 初始化样式
    font = xlwt.Font()  # 为样式创建字体
    font.name = name
    font.bold = bold
    font.color_index = 4
    font.height = height

    style.font = font
    return style


def write_xls_excel(path, season):
    print(seasons)
    # 创建工作簿
    workbook = xlwt.Workbook(encoding='utf-8')
    # 创建sheet
    data_sheet = workbook.add_sheet('番剧国创推荐')

    index = 0
    for i in season:
        data_sheet.write(index, 0, i['season'])
        for j in i['seasons']:
            data_sheet.write(index, 1, j)
            index += 1

    # 保存文件
    workbook.save(path)


def write_xlsx_excel(path, season, lack_index):
    outwb = openpyxl.Workbook()  # 打开一个将写的文件
    # outws = outwb.create_sheet(index=0)  # 在将写的文件创建sheet
    outws = outwb.active  # 在将写的文件创建sheet
    outws.title = "番剧国创推荐"

    outws.cell(1, 1).value = '原season'  # 写文件
    outws.cell(1, 2).value = 'ai推荐'  # 写文件
    outws.cell(1, 7).value = 'itemcf推荐'  # 写文件
    index = 2
    season_index = 0
    skip = []
    for single_seasons in season:
        if season_index in lack_index:
            season_index += 1
            skip.append(index)
            index += 20

        outws.cell(index, 1).value = single_seasons['season']  # 写文件
        for i in range(20):
            try:
                outws.cell(index, 2).value = single_seasons['seasons'][i]  # 写文件
            except:
                outws.cell(index, 2).value = ""  # 写文件
            index += 1
        season_index += 1

    print(u'skip:', skip)
    outwb.save(path)  # 一定要记得保存


if __name__ == '__main__':

    template_file = '/Users/lingzhengxin/Documents/jp_chn_season'
    path = '/Users/lingzhengxin/Documents/recommendSeasonAI.xlsx'
    url = 'http://api.bilibili.com/pgc/season/app/related/recommend'

    write_season = []
    seasonDict = read_season(template_file=template_file)

    index = 0
    lack_index = []
    for season_id, title in seasonDict.items():
        response = send_request('get', url=url, season_id=season_id)
        response = json.loads(response)
        time.sleep(0.003)
        print(response)
        oriSeason = seasonDict[season_id]
        seasons = []

        try:
            for season in response["result"]["season"]:
                seasons.append(season["title"])
                print(seasons)
            write_season.append({'season': oriSeason, 'seasons': seasons})
        except:
            try:
                for season in response["result"]["season"]:
                    seasons.append(season["title"])
                    print(seasons)
                write_season.append({'season': oriSeason, 'seasons': seasons})
            except:
                lack_index.append(index)
        index += 1
        # if index == 100:
        #     break

    print(lack_index)
    # write_xls_excel(path=path, write_season=write_season)
    write_xlsx_excel(path=path, season=write_season, lack_index=lack_index)
    print(u'创建recommendSeason.xlsx文件成功')
